﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 职工端
{
    public partial class 注册 : Form
    {
        public string strcon = denglu.strcon;
        SqlDataAdapter myda;
        DataSet myst = new DataSet();
        public 注册()
        {
            InitializeComponent();
            sx();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            SqlConnection sqlcon = new SqlConnection(strcon);
            string Waccount = this.textBox1.Text;
            string Wpassword = this.textBox2.Text;
            string Wname = this.textBox3.Text;
            string Wnumber = this.textBox4.Text;

            if (Waccount.Equals("") || Wpassword.Equals("") || Wname.Equals("") || Wnumber.Equals(""))
            {
                MessageBox.Show("信息不完整！");
            }
            
            try
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "INSERT INTO Worker(Waccount,Wpassword,Wname,Wnumber)VALUES('" + Waccount+ "','" +Wpassword+ "','" +Wname+  "','" +Wnumber+ "')";
                command.CommandType = CommandType.Text;
                command.Connection = sqlcon;
                sqlcon.Open();
                myda = new SqlDataAdapter(command);
                myda.Fill(myst, "Worker");
                MessageBox.Show("注册成功！", "消息", MessageBoxButtons.OK);
                denglu next = new denglu();//创建将要打开的窗体对象
                next.Show(); // 打开新窗体--Show非模式对话框--ShowDialog模式对话框
                this.Hide(); // this.Close()关闭当前窗体,因为登录是主窗体所以不能关闭
            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sqlcon.Close();
                sx();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
        }
        void sx()//刷新表格
        {
            using (SqlConnection sqlcon=new SqlConnection(strcon))
            {
                sqlcon.Open();
                string sql = "select Waccount as 账号 from Worker";
                SqlCommand sqlcmd = new SqlCommand(sql, sqlcon);
                myda = new SqlDataAdapter(sqlcmd);
                myst.Tables.Clear();
                myda.Fill(myst, "Worker");
                dataGridView1.DataSource = myst.Tables["Worker"];
            }
        }
    }

}

